<?php

namespace App\Console\Commands;

use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\ArticleType;
use App\Models\Family;
use App\Models\FamilyEdu;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\HospitalAdministrator;
use App\Models\HospitalAdminUserHospital;
use App\Models\HospitalTeam;
use App\Models\HospitalUserFamily;
use App\Models\old\Article;
use App\Models\old\ArticleInfo;
use App\Models\old\Autograph;
use App\Models\old\DoctorHospital;
use App\Models\old\DoctorParent;
use App\Models\old\HospitalAdminRoleUsers;
use App\Models\old\HospitalAdminUser;
use App\Models\old\Interview;
use App\Models\old\OldDoctor;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\Pregnancy;
use App\Models\old\UserLogin;
use App\Models\old\Weopenid;
use App\Models\ScanRecord;
use App\Models\TempPushRecord;
use App\Wechat\MyAccessToken;
use App\Wechat\Temp;
use Illuminate\Console\Command;

class migrationQrcode extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:qrcode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $weOpenids = Weopenid::query()->limit(1000)->get();
        foreach ($weOpenids as $weOpenid) {

            $row['openid'] = $weOpenid->openid;
            $row['hospital_id'] = $weOpenid->doctorid;
            $row['unionid'] = $weOpenid->unionid;
            $row['xopenid'] = $weOpenid->xopenid;
            $row['created_at'] = date('Y-m-d H:i:s',$weOpenid->createtime);

            $userLoginQuery = \App\Models\UserLogin::query();
            if ($weOpenid->unionid) {
                $userLoginQuery->where('unionid', $weOpenid->unionid);
            } elseif ($weOpenid->xopenid) {
                $userLoginQuery->where('xopenid', $weOpenid->xopenid);
            }
            $userLogin = $userLoginQuery->first();
            $user_id = $userLogin?$userLogin->user_id:0;
            $row['user_id'] = $user_id;

            $hospital = Hospital::query()->where('id', $row['hospital_id'])->first();
            $row['independent'] = $hospital->independent;
            $sign = ScanRecord::query()->firstOrCreate($row);
        }
        return 0;
    }
}
